<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reduce</title>
</head>
<body>
    <script>
        function myReduce(arr, fn, initFlag){
            // arr.reduce((before, curr, index, arr) => , init)
            if(!initFlag){
                initFlag = arr[0];
                arr = arr.splice(1);
            } 
            for (let index = 0; index < arr.length; index++) {
                var result = fn.call(arr, initFlag, arr[index], index, arr);
                initFlag = result;
            }
            return initFlag;
        }

        console.log(myReduce([1,2,3,4], (before, curr, index, arr) => {
            console.log(before, curr, index, arr);
            return before + curr;
        }, 15));
    </script>
</body>
</html>